package subscription

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	subscriptionDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/subscription"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/business_opportunity/subscription"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type SubscriptionController struct {
	controller.BaseController
	subscriptionService subscription.SubscriptionServiceIface
}

func NewSubscriptionController(
	subscriptionService subscription.SubscriptionServiceIface,
) *SubscriptionController {
	return &SubscriptionController{
		subscriptionService: subscriptionService,
	}
}

// RegisterRouter
// 注册路由
func (c *SubscriptionController) RegisterRouter(g *gin.RouterGroup) {
	//订阅相关
	//g.POST("/apply-cron", c.ApplyCron)//下线，但不删除
	g.GET("/get-radar-subscription-config", c.GetRadarSubscriptionConfig)
	g.POST("/save-radar-subscription-config", c.SaveRadarSubscriptionConfig)
	g.GET("/get-subscription-push-config", c.GetSubscriptionPushConfig)
	g.POST("/save-subscription-push-config", c.SaveSubscriptionPushConfig)
	g.GET("/get-wx-qr-code", c.GetWxQrCode)
	g.GET("/get-bind-status", c.GetBindStatus)
	g.POST("/bind-openid-by-card", c.BindOpenIdByCard)

	g.POST("get-radar-list", c.getRadarList)
	g.POST("radar-subscription-pre-count", c.getRadarPreCount)
}

/*
*ApplyCron 目前仍然初始化xxl-job，有新的调度服务再切换或者废弃
 */

/*
func (c *SubscriptionController) ApplyCron(ctx *gin.Context) {
	request := &subscriptionDto.EmptyRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	rs, err := c.subscriptionService.ApplyCron(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)

}*/

func (c *SubscriptionController) GetRadarSubscriptionConfig(ctx *gin.Context) {
	request := &subscriptionDto.GetRadarSubscriptionConfigRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Error(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	rs, err := c.subscriptionService.GetRadarSubscriptionConfig(ctx, request)
	if err != nil {
		stark.Logger.Error(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, rs)
}

func (c *SubscriptionController) SaveRadarSubscriptionConfig(ctx *gin.Context) {
	var request = &subscriptionDto.SaveRadarSubscriptionConfigRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	rs, err := c.subscriptionService.SaveRadarSubscriptionConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *SubscriptionController) GetSubscriptionPushConfig(ctx *gin.Context) {
	var request = &subscriptionDto.GetSubscriptionPushConfigRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	rs, err := c.subscriptionService.GetSubscriptionPushConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	if rs.Id == 0 {
		c.Success(ctx, nil)
	} else {
		c.Success(ctx, rs)
	}

}

func (c *SubscriptionController) SaveSubscriptionPushConfig(ctx *gin.Context) {
	var request = &subscriptionDto.SaveSubscriptionPushConfigRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	rs, err := c.subscriptionService.SaveSubscriptionPushConfig(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *SubscriptionController) GetWxQrCode(ctx *gin.Context) {
	var request = &subscriptionDto.GetWxQrCodeRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}
	globalUserId := authorize.GetGlobalUserId(ctx)
	if globalUserId == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "用户编号为空")
		return
	}

	rs, err := c.subscriptionService.GetWxQrCode(ctx, request, globalUserId)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *SubscriptionController) GetBindStatus(ctx *gin.Context) {
	var request = &subscriptionDto.GetBindStatusRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	rs, err := c.subscriptionService.GetBindStatus(ctx, request.SceneID)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *SubscriptionController) BindOpenIdByCard(ctx *gin.Context) {
	var request = &subscriptionDto.BindOpenIdByCardRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_PARAMS_ERROR, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数异常")
		return
	}

	rs, err := c.subscriptionService.BindOpenIdByCard(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}

	c.Success(ctx, rs)
}

func (c *SubscriptionController) getRadarList(ctx *gin.Context) {
	var request subscriptionDto.GetRadarListRequest
	if err := ctx.ShouldBind(&request); err != nil {
		stark.Logger.Errorf(ctx, "http getRadarList 参数错误：%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	resp, err := c.subscriptionService.GetRadarList(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "http getRadarList 参数：%#v, 错误：%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, "获取数据失败")
		return
	}
	c.Success(ctx, resp)
}

func (c *SubscriptionController) getRadarPreCount(ctx *gin.Context) {
	var request subscriptionDto.GetRadarPreCountRequest
	if err := ctx.ShouldBind(&request); err != nil {
		stark.Logger.Errorf(ctx, "http getRadarPreCount 参数错误：%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}

	resp, err := c.subscriptionService.GetRadarPreCount(ctx, &request)
	if err != nil {
		stark.Logger.Errorf(ctx, "http getRadarPreCount 参数：%#v, 错误：%s", request, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, "获取数据失败")
		return
	}
	c.Success(ctx, resp)
}
